package com.example.supernote.ui.settings


import android.content.Context
import android.widget.Toast
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.CenterAlignedTopAppBar
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.example.supernote.R
import com.example.supernote.model.settings.LanguageType
import com.example.supernote.model.settings.ModeType
import com.example.supernote.ui.theme.SuperNoteTheme

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun SettingsScreen(
    uiState: State<SettingsUiState>,
    changeModel: (selectModel: ModeType) -> Unit,
    changeLanguage: (selectLanguage: LanguageType, context: Context) -> Unit,
    isExpandedScreen: Boolean,
    openDrawer: () -> Unit,
    snackbarHostState: SnackbarHostState
) {
    val context = LocalContext.current
    Scaffold(
        snackbarHost = { SnackbarHost(hostState = snackbarHostState) },
        topBar = {
            CenterAlignedTopAppBar(
                title = {
                    Text(
                        text = stringResource(R.string.cd_settings),
                        style = MaterialTheme.typography.titleLarge
                    )
                },
                navigationIcon = {
                    if (!isExpandedScreen) {
                        IconButton(onClick = openDrawer) {
                            Icon(
                                painter = painterResource(R.drawable.ic_super_note_logo),
                                contentDescription = stringResource(
                                    R.string.cd_open_navigation_drawer
                                ),
                                modifier = Modifier.size(32.dp),
                                tint = MaterialTheme.colorScheme.primary
                            )
                        }
                    }
                },
                actions = {
                    IconButton(
                        onClick = {
                            Toast.makeText(
                                context,
                                "Search is not yet implemented in this configuration",
                                Toast.LENGTH_LONG
                            ).show()
                        }
                    ) {
                        Icon(
                            imageVector = Icons.Filled.Search,
                            contentDescription = stringResource(R.string.cd_search)
                        )
                    }
                }
            )
        }
    ) { innerPadding ->
        val screenModifier = Modifier.padding(innerPadding)
        SuperNoteSettings(
            uiState = uiState,
            changeModel = changeModel,
            changeLanguage = changeLanguage,
            modifier = screenModifier,
        )
    }
}


//@Preview(showBackground = true)
//@Composable
//fun PreviewSettingsScreen() {
//    val viewModel = SettingsViewModel()
//    val uiState: State<SettingsUiState> = viewModel.uiState.collectAsStateWithLifecycle()
//    SuperNoteTheme {
//        SettingsScreen(
//            uiState = uiState,
//            changeModel = { viewModel.changeModel(it) },
//            changeLanguage = { viewModel.changeLanguage(it) },
//            isExpandedScreen = false,
//            openDrawer = { },
//            snackbarHostState = SnackbarHostState()
//        )
//    }
//}